package com.dlb.andorid.activity;

import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.bean.MyFav;
import com.dlb.andorid.presenter.CollectPresenter;
import com.kwan.xframe.common.adatper.CommonViewPageAdapter;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonPageActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyCollectActivity extends BaseCommonPageActivity implements OnLoadMoreListener, OnRefreshListener, ViewPager.OnPageChangeListener, XContract.ICollectView {

	CollectPresenter mPresenter;

	protected String[] tits = new String[]{"宝贝", "店铺"};

	private List<MyFav> mShops = new ArrayList<>();
	private List<MyFav> mProducts = new ArrayList<>();

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new CollectPresenter(this);
		return mPresenter;
	}

	@Override
	protected String getTitleTxt() {
		return "我的收藏";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	protected void initViews() {
		super.initViews();
		setRecycle();
		mViewPager.addOnPageChangeListener(this);
	}

	SmartRefreshLayout sl_product, sl_shop;
	private ObservableRecyclerView rv_product, rv_shop;

	BaseQuickAdapter<MyFav, BaseViewHolder> mProductAdapter;
	BaseQuickAdapter<MyFav, BaseViewHolder> mShopAdapter;

	private void setRecycle() {

		View view_product = getLayoutInflater().inflate(R.layout.layout_base_recycle, null);
		View view_shop = getLayoutInflater().inflate(R.layout.layout_base_recycle, null);

		sl_product = view_product.findViewById(R.id.refreshLayout);
		sl_shop = view_shop.findViewById(R.id.refreshLayout);

		sl_product.setOnLoadMoreListener(this);
		sl_shop.setOnLoadMoreListener(this);

		sl_product.setOnRefreshListener(this);
		sl_shop.setOnRefreshListener(this);


		rv_product = view_product.findViewById(R.id.recycleView);
		rv_shop = view_shop.findViewById(R.id.recycleView);

		initRecycler(rv_product);
		initRecycler(rv_shop);

		mProductAdapter = new BaseQuickAdapter<MyFav, BaseViewHolder>(R.layout.list_item_collect_shop, mProducts) {
			@Override
			protected void convert(BaseViewHolder helper, MyFav item) {

			}
		};

		rv_product.setAdapter(mProductAdapter);


		mShopAdapter = new BaseQuickAdapter<MyFav, BaseViewHolder>(R.layout.list_item_collect_shop, mShops) {
			@Override
			protected void convert(BaseViewHolder helper, MyFav item) {

			}
		};

		rv_shop.setAdapter(mShopAdapter);


		ArrayList<View> vs = new ArrayList<>();
		vs.add(view_product);
		vs.add(view_shop);
		List<String> titles = Arrays.asList(tits);
		setUpViewPage(new CommonViewPageAdapter<>(vs, titles), false, 18, getResources().getColor(R.color.colorPrimary), Color.BLACK);
	}

	private void initRecycler(RecyclerView recyclerView) {

		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		recyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		recyclerView.setHasFixedSize(true);
		//mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		//解决更新 闪烁
		recyclerView.setItemAnimator(null);
		recyclerView.setNestedScrollingEnabled(false);

	}

	@Override
	protected void initData() {
		super.initData();
		showProgress("");
		mPresenter.getFav(1);
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

	}

	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {

	}

	@Override
	public void onPageScrolled(int i, float v, int i1) {

	}

	int mCurrentPageSelected;

	@Override
	public void onPageSelected(int i) {
		mCurrentPageSelected = i;
		switch (mCurrentPageSelected) {
			case 0:
				if (mProducts.size() == 0) {
					mPresenter.getFav(1);
				}
				break;
			case 1:
				if (mShops.size() == 0) {
					mPresenter.getFav(2);
				}
				break;
		}

	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}

	@Override
	public void getFavSuccess(List<MyFav> data, int type) {
		dismissProgress();

		switch (type) {

			case 1://商品
				mProducts.clear();
				mProducts.addAll(data);
				mProductAdapter.setNewData(data);
				break;

			case 2://店铺

				mShops.clear();
				mShops.addAll(data);
				mShopAdapter.setNewData(data);

				break;

		}
	}

	@Override
	public void addFavSuccess(XServerMsg data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}

	@Override
	public void delFavSuccess(XServerMsg data) {
		toastMsg(data.getMsg());
		switch (mCurrentPageSelected) {
			case 0:
				mPresenter.getFav(1);
				break;
			case 1:
				mPresenter.getFav(2);
				break;
		}
	}
}
